<?php

/* 
 * 教师留言
 */

namespace Admin\Controller;

class TeachersController extends ComController
{
    //留言列表
    public function massage(){
        $status= intval($_GET['status'])?$_GET['status']:'';
        $where['sc_massage.uid'] = $_SESSION['uid'];
        if($status){
            $where['sc_massage.status'] = $status;
        }
        $p= intval($_GET['p'])?$_GET['p']:1;
        $pagesize = 15;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $data = M('massage')->field("sc_massage.*,sc_user.name")->join(" sc_user on sc_user.id = sc_massage.user_id")->where($where)->limit($offset . ',' . $pagesize)->order('sc_massage.add_time desc')->select();
        $count = M('massage')->where($where)->count();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('page',$page);
        $this->assign('data',$data);
        $this->display();
    }
    //删除留言
    public function del(){
        $id = $_GET['id'];
        $res = M('massage')->where(array('id'=>$id))->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //回复留言
    public function answer(){
        $user_id = $_GET['user_id'];
        $uid = $_GET['uid'];
        $data['user_id']=$user_id;
        $data['uid'] = $uid;
        $data['content'] = $_GET['content'];
        $data['massage_id'] = $_GET['id'];
        if(empty($data['content'])){
            echo 2;//回复内容为空
        }
        $data['add_time'] = date('Y-m-d H:i:s');
        $res = M('answer')->add($data);
        if($res){
            //修改留言状态
            $res = M('massage')->where(array('id'=>$_GET['id']))->save(array('status'=>1));
            if(res === false){
                echo 0;//失败
            }else{
                echo 1;//成功
            }
        }else{
            echo 0;//失败
        }
    }
}